<?php

function heivrid($admin = array()){
	return $admin['realname'] ? $admin['realname'] : $admin['username'];
}

function get_province(){
	return limitquery('district', array('level' => 1));
}
function get_city($pid = 0){
	return limitquery('district', array('level' => 2, 'pid' => $pid));
}
function get_district($pid = 0){
	return limitquery('district', array('level' => 3, 'pid' => $pid));
}

function get_province_name($pid = 0){
	$province = fetch('district', array('id' => $pid, 'level' => 1));
	return $province['name'];
}

function get_city_name($cid = 0){
	$city = fetch('district', array('id' => $cid, 'level' => 2));
	return $city['name'];
}

function contact_types(){
	$types = array(
		'phone' => '电话',
		'qq' => 'QQ',
		'email' => '邮件',
		'face' => '面谈'
	);
	return $types;
}

function customer_jobs(){
	$jobs = array(
		'总经理', '合伙人', '销售经理', '项目负责人', '主管', '销售策划', '个人', '其他'
	);
	return $jobs;
}

function customer_status(){
	$status = array(
		'潜在客户', '目标客户', '商谈客户', '签约客户'
	);
	return $status;
}

function customer_demand(){
	$status = array(
		'ska' => '神卡王国',
		'ipad_sale' => 'IPAD售楼',
		'3D' => '三维动画', 
		'touch' => '触摸屏', 
		'AR' => '增强现实', 
		'VR' => '虚拟现实',
		'pad' => 'PAD产品',
		'phone_app' => '手机APP',
		'effect' => '效果图',
		'interactive' => '互动投影',
		'hall' => '环幕及影音厅',
		'wx' => '微信',
		'holographic' => '全息',
		'alliance' => 'AR联盟',
		'partner' => '合作商',
		'helmet' => '虚拟头盔',
		'other' => '其他'
	);
	return $status;
}

function customer_source(){
	$sources = array(
		'baidu' => '百度',
		'weibo' => '微博',
		'weixin' => '微信',
		'email' => '邮件',
		'phone' => '电话',
		'alibaba' => '阿里巴巴',
		'videosite' => '视频网站',
		'qqgroup' => 'QQ群',
		'bbs' => '论坛',
		'invite' => '推荐',
		'dev' => '自主开发',
		'other' => '其他'
	);
	return $sources;
}

function customer_nature(){
	$natures = array(
		'dh' => '动画公司',
		'dcch' => '地产策划',
		'gg' => '广告（非地产）',
		'soft' => '软件',
		'cp' => '产品企业',
		'dcgs' => '地产公司',
		'dlgs' => '代理公司',
		'spmx' => '沙盘模型公司',
		'gggs' => '公关公司',
		'other' => '其他'
	);
	return $natures;
}

function confirm_password($admin = array(), $password = ''){
	if( !$admin['uac'] && UAC_LEVEL < 1 ){
		return true;
	}
	$account = new admincp_account();
	return $account->check_password($admin, $password);
}

function moneyit($m = 0) {
	return rtrim(rtrim(sprintf('%.2f', $m), '0'), '.');
}

function get_parse_companys($users = array()){

	$companys = limitquery('companys');

	$users = $users ? $users : limitquery('admincp_member');
	$groups = &$admincp->groups;
	
	$departments = parse_array(limitquery('admincp_department'));
	$companys = parse_array($companys);
	
	foreach( $users as $k => $v ){
		$companys[$v['companyid']]['users'][$v['id']] = $v;
	}

	foreach( $departments as $k => $v ){
		if( $v['companyid'] ){
			$companys[$v['companyid']]['sub_department'][$v['id']] = $v;
		}else{
			foreach( $companys as $comk => $comv ){
				$companys[$comk]['sub_department'][$v['id']] = $v;
			}
		}
	}

	foreach( $companys as $k => $v ){
		foreach( $v['users'] as $userk => $userv ){
			if( $userv['departmentid'] ){
				$companys[$k]['sub_department'][$userv['departmentid']]['users'][$userv['id']] = $userv;
				unset($companys[$k]['users'][$userk]);
			}
		}
		foreach( $v['sub_department'] as $departk => $departv ){
			if( $departv['parentid'] ){
				$companys[$k]['sub_department'][$departv['parentid']]['sub_department'][$departv['id']] = $departv;
				unset($companys[$k]['sub_department'][$departk]);
			}
		}
				
		if( !$companys[$k]['users'] ){
			unset($companys[$k]['users']);
		}
		if( !$companys[$k]['sub_department'] ){
			unset($companys[$k]['sub_department']);
		}
	}

/*


	foreach( $departments as $k => $v ){
		if( $v['parentid'] ){
			$departments[$v['parentid']]['sub_department'][$v['id']] = $v;
			unset($departments[$v['id']]);
		}
	}

	foreach( $companys as $k => $v ){
		if( $v['parentid'] ){
			$companys[$v['parentid']]['sub_company'][$v['id']] = $v;
			unset($companys[$v['id']]);
		}
	}
*/
	return $companys;
}
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
	$ckey_length = 4;
	$key = md5($key != '' ? $key : 'globalkey');
	$keya = md5(substr($key, 0, 16));
	$keyb = md5(substr($key, 16, 16));
	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

	$cryptkey = $keya.md5($keya.$keyc);
	$key_length = strlen($cryptkey);

	$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
	$string_length = strlen($string);

	$result = '';
	$box = range(0, 255);

	$rndkey = array();
	for($i = 0; $i <= 255; $i++) {
		$rndkey[$i] = ord($cryptkey[$i % $key_length]);
	}

	for($j = $i = 0; $i < 256; $i++) {
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}

	for($a = $j = $i = 0; $i < $string_length; $i++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}

	if($operation == 'DECODE') {
		if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
			return substr($result, 26);
		} else {
			return '';
		}
	} else {
		return $keyc.str_replace('=', '', base64_encode($result));
	}
}
function dstrpos($string, &$arr, $returnvalue = false) {
	if(empty($string)) return false;
	foreach((array)$arr as $v) {
		if(strpos($string, $v) !== false) {
			$return = $returnvalue ? $v : true;
			return $return;
		}
	}
	return false;
}

function checkmobile() {
	global $_G;
    $base = false;
	$mobile = array();
	static $mobilebrowser_list =array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
				'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
				'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
				'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
				'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
				'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
				'benq', 'haier', '^lct', '320x320', '240x320', '176x220');
	$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
	if(($v = dstrpos($useragent, $mobilebrowser_list, true))) {
		$_G['mobile'] = $v;
		return true;
	}
	$brower = array('mozilla', 'chrome', 'safari', 'opera', 'm3gate', 'winwap', 'openwave', 'myop');
	if(dstrpos($useragent, $brower)) return false;

	$_G['mobile'] = 'unknown';
    if(@$_GET['mobile'] != 'no') {
	   @$_GET['mobile'] = 'yes';
	}    
	if($_GET['mobile'] === 'yes' && $base) {
		return true;
	} else {
		return false;
	}
}

function HumanTime($time = 0, $forceDate = false){
	$now = time();
	$time = is_numeric($time) ? $time : strtotime($time);

	$interval = $now - $time;

	if ( $forceDate || $interval > 30*86400 ){
	    return strftime("%Y-%m-%d",$time);
	}else if ( $interval > 86400 ){
	    $number = intval($interval/86400);
	    return "${number}天前";
	}else if ( $interval > 3600 ){ // > 1 hour
	    $number = intval($interval/3600);
	    return "${number}小时前";
	}else if ( $interval >= 60 ){ // > 1 min
	    $number = intval($interval/60);
	    return "${number}分钟前";
	}else if ( 5 >= $interval){// < 5 second
	    return "就在刚才";
	}else{ // < 1 min
	   return "${interval}秒前";
	}
}
function down_xls($data = array(), $keynames = array(), $name = 'dataxls') {
	$xls[] = "<html><meta http-equiv=content-type content=\"text/html; charset=UTF-8\"><body><table border='1'>";
	$xls[] = "<tr><td>序号</td><td>" . implode("</td><td>", array_values($keynames)) . '</td></tr>';
	foreach($data as $o) {
		$line = array(++$index);
		foreach($keynames as $k => $v) {
			$line[] = $o[$k];
		}
		$xls[] = '<tr><td>'. implode("</td><td>", $line) . '</td></tr>';
	}
	$xls[] = '</table></body></html>';
	$xls = join("\r\n", $xls);
	header('Content-Disposition: attachment; filename="'.$name.'.xls"');
	die(mb_convert_encoding($xls, 'UTF-8', 'UTF-8'));
}
?>
